//
//  ViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/11/27.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "ViewController.h"
#import "TextViewController.h"
#import "QiniuPicUPload.h"
#import <QiniuSDK.h>
#import <TZImagePickerController.h>
#import "ImageExamineViewController.h"
#import "HomeViewController.h"

@interface ViewController () <TZImagePickerControllerDelegate>

@property (nonatomic, strong) NSMutableArray *iamgeMArray;
@property (nonatomic, strong) NSMutableArray *artworkMArray;

@property (nonatomic, strong) NSMutableArray *keysMArray;

@property (nonatomic, strong) NSMutableArray *imageKey;
@property (nonatomic) BOOL orUpdata;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    
    NSMutableArray *array = [NSMutableArray array];
    NSDictionary *dic = [NSDictionary dictionary];
//    QiniuPicUPload *pload = [[QiniuPicUPload alloc] initImage:array QniuToken:@"token" QiniuParameters:dic];
//    [pload start];
//    pload.callBackBlock = ^(BOOL can) {
//        if (can) {
//            NSLog(@"上传成功");
//        }else {
//            NSLog(@"上传失败");
//        }
//    };
    self.iamgeMArray = [NSMutableArray array];
    self.artworkMArray = [NSMutableArray array];
    self.keysMArray = [NSMutableArray array];
    self.orUpdata = NO;
}

- (IBAction)pushAction:(id)sender {
    [self.navigationController pushViewController:[TextViewController new] animated:YES];
}

- (IBAction)pickImageView:(id)sender {
    TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initWithMaxImagesCount:3 delegate:self];
    imagePicker.allowPickingOriginalPhoto = YES;
    imagePicker.allowPickingVideo = NO;
    imagePicker.allowPickingImage = YES;
    imagePicker.showSelectedIndex = YES;
    imagePicker.showPhotoCannotSelectLayer = YES;
    imagePicker.cannotSelectLayerColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    imagePicker.allowTakePicture = NO;
//    imagePicker.preferredLanguage = @"zh-Hans";
    // 这是一个navigation 只能present
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark --pickImageDelegate--
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    NSLog(@"缩略photos----%@", photos);
    NSLog(@"原图assets----%@", assets);
    self.iamgeMArray = photos.copy;
    self.artworkMArray = assets.copy;
    
}

- (IBAction)imageDispose:(id)sender {
    ImageExamineViewController *imageExamine = [ImageExamineViewController initWithImageExamineViewController:self.iamgeMArray arrtwork:self.artworkMArray];
    [self.navigationController pushViewController:imageExamine animated:YES];
}

- (IBAction)uploadActionOne:(id)sender {
    [self uploadImages:self.iamgeMArray atIndex:0 token:@"RCYJEHvGdufWjrrbpq3rcMuCDM7vpPyBgxLFuSJv:-iB3FGrjHf7YzgkuKXKcaFy2gIg=:eyJzY29wZSI6ImhvbmV5d2ViIiwiZGVhZGxpbmUiOjI2MzQ1MzQxODl9" uploadManager:[[QNUploadManager alloc] init] keys:self.keysMArray];
}

- (IBAction)uploadActionTwo:(id)sender {
    if (self.orUpdata == NO) {
        //图片上传之后
        [self. navigationController pushViewController:[HomeViewController initWithImageHomeViewController:self.imageKey arrtwork:self.artworkMArray] animated:YES];
    }else {
        NSLog(@"图片上传尚未成功");
    }

}

- (IBAction)uploadActionThree:(id)sender {
    
}


- (void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager:(QNUploadManager *)uploadManager keys:(NSMutableArray *)keys{
    if (images.count <= 0) {
        return;
    }
    UIImage *image = images[index];
    __block NSInteger imageIndex = index;
    //图片转化是否有损
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    //时间戳
    NSTimeInterval time= [[NSDate new] timeIntervalSince1970];
    //图片路径拼接
    NSString *filename = [NSString stringWithFormat:@"%@_%ld_%.f.%@",@"status",686734963504054272,time,@"jpg"];
    [uploadManager putData:data key:filename token:token
                  complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                      if (info.isOK) {
                          NSString *imageUrl = [NSString stringWithFormat:@"http://web.wohoney.com/%@", key];
                          [keys addObject:imageUrl];
                          NSLog(@"idInex %ld,OK",index);
                          imageIndex++;
                          if (imageIndex >= images.count) {
                              NSLog(@"上传完成");
                              self.imageKey = [NSMutableArray arrayWithArray:keys];
                              self.orUpdata = YES;
                              for (NSString *imgKey in keys) {
                                  NSLog(@"%@",imgKey);
                              }
                              
                              return ;
                          }
                          [self uploadImages:images atIndex:imageIndex token:token uploadManager:uploadManager keys:keys];
                      }
                      
                  }
    option:nil];
}

- (NSArray *)forImageURL:(NSMutableArray *)imageKey {
    NSMutableArray *array = [NSMutableArray array];
    if (imageKey.count > 0) {
        for (int i = 0; i < imageKey.count; i++) {
            NSDictionary *dic = [NSDictionary dictionary];
            NSString *key = imageKey[i];
            NSString *imageUrl = [NSString stringWithFormat:@"http://web.wohoney.com/%@", key];
            [dic setValue:imageUrl forKey:[NSString stringWithFormat:@"image%d", i]];
            [array addObject:dic];
        }
        NSLog(@"数据源--%@", array);
        return [NSArray arrayWithArray:array];
    }else {
        return nil;
    }
}

@end
